package com.yx.study.leet.utils;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode(int... values) {
        for (int val : values) {
            addNextNode(this, val);
        }
        ListNode node = this.next;
        this.val = node.val;
        this.next = node.next;
    }

    private void addNextNode(ListNode node, int value) {
        if (node.next == null) {
            node.next = new ListNode(value);
        } else {
            addNextNode(node.next, value);
        }
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[").append(val);
        if (next != null) {
            getNextVal(next, stringBuilder);
        }
        return stringBuilder.append("]").toString();
    }

    public StringBuilder getNextVal(ListNode node, StringBuilder stringBuilder) {
        stringBuilder.append(",").append(node.val);
        if (node.next != null) {
            getNextVal(node.next, stringBuilder);
        }
        return stringBuilder;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}